<?php
class LoginController{
	private $_obj        = null;
	private $_urlPrefix = 'index.php?p=admin&c=';
	
	public function __construct() {
		$this -> _obj = new UserModel();
	}
	
	public function indexAction() {
		include __VIEW__ . PLATFORM . DS . 'login.html';
	}
	
	public function loginAction() {
		$user  = $_POST['user'];
		$pwd   = $_POST['pwd'];
		$verify = $_POST['verify'];
		$controllerObj = new Controller();
		
		if($verify != $_SESSION['verify']) {
			$controllerObj -> jump($this -> _urlPrefix . 'login&a=index', '验证码不正确');
		}
		
		$users = $this -> _obj ->getUser($user);	
		
		if($users && $users[0]['password'] == md5($pwd)) {
			$_SESSION['uid']  = $users[0]['u_id'];
			$_SESSION['user'] = $users[0]['user'];
			
			$controllerObj -> jump($this -> _urlPrefix . 'index&a=index', '登陆成功');
		}else {
			$controllerObj -> jump($this -> _urlPrefix . 'login&a=index', '用户名密码不正确');
		}
	}
	
	public function captchaAction() {
		require VENDOR_PATH . 'autoload.php';

		$builder = new Gregwar\Captcha\CaptchaBuilder();
		$builder->build(120, 40);
		$_SESSION['verify'] = $builder->getPhrase();
		
		header('Content-type: image/jpeg');
		$builder->output();
	}
}
?>